<?php
namespace app\manager\controller;

use app\admin\model\Admins;
use think\File;
use think\Paginator;


Class Index extends Base
{
	Public function index(){
    
      $ftime =date('Y-m-d',time());
      $frtime =strtotime($ftime);
      $time =time();
      $login = $_SESSION['Msg'];

     if(!isset($_POST['key'])){

      $res =Admins::table('wolive_service')->distinct(true)->field('business_id')->paginate(8);

      $data=[];

      foreach ($res as $vg) {
          $v =$vg['business_id'];
          $cdata=[];
          // 正在谈话人数
          $talknum =Admins::table('wolive_queue')->where(['business_id'=>$login['business_id']])->where('state','normal')->where("service_id",'<>',0)->count();
          // 在线客服人数
          $services=Admins::table("wolive_service")->where(['business_id'=>$login['business_id'],'state'=>'online'])->count();
          // 正在排队人数
          $waitnum =Admins::table("wolive_queue")->where(['business_id'=>$login['business_id']])->where("service_id",0)->count();
          // 接入总人数
          $totalvisit=Admins::table("wolive_chats")->distinct(true)->field('visiter_id')->where('business_id',$login['business_id'])->count();
          // 今日会话量
          $nowchat = Admins::table("wolive_chats")->where('business_id',$login['business_id'])->where('timestamp','like',"{$time}%")->count();
          // 会话总量
          $totalchat =Admins::table('wolive_chats')->where('business_id',$v)->count();
          // 今日留言量
          $nowmsg = Admins::table('wolive_message')->where('business_id',$login['business_id'])->where('timestamp','like',"{time}%")->count();
          // 留言总量
          $totalmsg = Admins::table('wolive_message')->where('business_id',$login['business_id'])->count();
          
          $web =Admins::table('wolive_business')->where('business_id',$v)->find();
          
          if($web){
            $cdata['state'] ='open';
          }else{
            $cdata['state']= 'close';
          }

          $cdata['web']=$v;
          $cdata['talk']=$talknum;
          $cdata['service']=$services;
          $cdata['wait']=$waitnum;
          $cdata['visited']=$totalvisit;
          $cdata['nowchat']=$nowchat;
          $cdata['totalchat']=$totalchat;
          $cdata['nowmsg']=$nowmsg;
          $cdata['totalmsg']=$totalmsg;
          $data[]=$cdata;
      }

      $page = $res->render();

      $this->assign("data",$data);
      $this->assign('page', $page);

     }else{



       $key =$_POST['key'];
       $res =Admins::table('wolive_service')->distinct(true)->field('business_id')->where('business_id','like','%'.$key.'%')->paginate(10);
       $data=[];

      foreach ($res as $vg) {
          $v =$vg['business_id'];
          $cdata=[];
          // 正在谈话人数
         $talknum =Admins::table('wolive_queue')->where(['business_id'=>$login['business_id']])->where('state','normal')->where("service_id",'<>',0)->count();
          // 在线客服人数
          $services=Admins::table("wolive_service")->where(['business_id'=>$login['business_id'],'state'=>'online'])->count();
          // 正在排队人数
          $waitnum =Admins::table("wolive_queue")->where(['business_id'=>$login['business_id']])->where("service_id",0)->count();
          // 接入总人数
          $totalvisit=Admins::table("wolive_chats")->distinct(true)->field('visiter_id')->where('business_id',$login['business_id'])->count();
          // 今日会话量
          $nowchat = Admins::table("wolive_chats")->where('business_id',$login['business_id'])->where('timestamp','like',"{$time}%")->count();    
          // 会话总量
          $totalchat =Admins::table('wolive_chats')->where('business_id',$v)->count();
          // 今日留言量
          $nowmsg = Admins::table('wolive_message')->where('business_id',$login['business_id'])->where('timestamp','like',"{time}%")->count();
          // 留言总量
          $totalmsg = Admins::table('wolive_message')->where('business_id',$login['business_id'])->count();

          $web =Admins::table('wolive_business')->where('business_id',$v)->find();

          if($web){
            $cdata['state'] ='open';
          }else{
            $cdata['state']= 'close';
          }


          $cdata['web']=$v;
          $cdata['talk']=$talknum;
          $cdata['service']=$services;
          $cdata['wait']=$waitnum;
          $cdata['visited']=$totalvisit;
          $cdata['nowchat']=$nowchat;
          $cdata['totalchat']=$totalchat;
          $cdata['nowmsg']=$nowmsg;
          $cdata['totalmsg']=$totalmsg;
          $data[]=$cdata;
      }

      $page = $res->render();

      $this->assign("data",$data);
      $this->assign('page', $page);

     }
      
    $this->assign('title','首页');

     
	  return $this->fetch();
	}
}